package com.ruoyi.audit.controller;

import com.ruoyi.audit.domain.Audit;
import com.ruoyi.audit.service.AuditService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.company.domain.Company;
import com.ruoyi.company.domain.CompanyInvoice;
import com.ruoyi.company.service.ICompanyInvoiceService;
import com.ruoyi.company.service.ICompanyServer;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.mateProcurement.domain.MateOutbound;
import com.ruoyi.mateProcurement.domain.MateOutboundModel;
import com.ruoyi.mateProcurement.mapper.MateOutboundMapper;
import com.ruoyi.mateProcurement.mapper.MateProcurementMapper;
import com.ruoyi.material.domain.MaterialManufactuter;
import com.ruoyi.product.domain.Product;
import com.ruoyi.sale.domain.SaleGoodsModel;
import com.ruoyi.sale.domain.SaleGoodsOrder;
import com.ruoyi.sale.service.ISaleGoodsService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 入库审核Controller
 *
 * @author zjh
 * @date 2022-05-31
 */
@RestController
@RequestMapping("/saleGoodsView")
public class SaleGoodsViewController extends BaseController {
    @Autowired

    MateOutboundMapper mateOutboundMapper;
    private String prefix = "inventoryView";
    @Autowired
    private ISaleGoodsService iSaleGoodsService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private ICompanyInvoiceService companyInvoiceService;
    @Autowired
    private ICompanyServer iCompanyServer;
    @Autowired
    private AuditService auditService;

    @Autowired
    private MateProcurementMapper mateProcurementMapper;

    /**
     * 销货单管理界面
     *
     * @return
     */
    @RequiresPermissions("business:saleGoodsView:view")
    @GetMapping()
    public String getlist() {
        return prefix + "/saleGoodsView";
    }

    /**
     * 销售订单管理界面
     */
    @RequiresPermissions("business:saleGoodsView:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo showlist(@RequestParam(value = "beginTime", required = false) String beginTime,
                                  @RequestParam(value = "endTime", required = false) String endTime) {
        SaleGoodsOrder saleGoodsOrder = new SaleGoodsOrder();
        saleGoodsOrder.setBeginTime(beginTime);
        saleGoodsOrder.setEndTime(endTime);
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        startPage();
        saleGoodsOrder.setDeptId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        if (!sysUser.getDeptId().equals(sysUser.getCompanyId())) {
            saleGoodsOrder.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        }
        saleGoodsOrder.setSaleGoodsStatus("1");
        List<SaleGoodsOrder> list = iSaleGoodsService.selectSaleGoodsShowList(saleGoodsOrder);
        return getDataTable(list);
    }

    @GetMapping("/matelist")
    @ResponseBody
    public TableDataInfo mateshowlist(@RequestParam(value = "beginTime", required = false) String beginTime,
                                      @RequestParam(value = "endTime", required = false) String endTime) {
        MateOutbound mateOutbound = new MateOutbound();
        mateOutbound.setBeginTime(beginTime);
        mateOutbound.setEndTime(endTime);
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        startPage();
        mateOutbound.setDeptId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        if (!sysUser.getDeptId().equals(sysUser.getCompanyId())) {
            mateOutbound.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        }
        mateOutbound.setMateGoodsStatus("1");
        List<MateOutbound> list = mateOutboundMapper.selectMateOutboundShowList(mateOutbound);
        return getDataTable(list);
    }

    /**
     * 修改销售订单
     */
    @GetMapping("/edit/{saleGoodsId}")
    public Map<String, Object> edit(@PathVariable("saleGoodsId") Long saleGoodsId) {
        Map<String, Object> resMap = new HashMap<>();
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long companyId = sysUser.getCompanyId();
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("saleGoodsId", saleGoodsId);
        hashMap.put("deptId", sysUser.getDeptId());

        SaleGoodsOrder saleGoodsOrder = iSaleGoodsService.selectSaleGoodsById(hashMap);
        Audit audit = new Audit();
        audit.setOrderId(saleGoodsId);
        audit.setOrderType("3");
        audit.setOrderNum(saleGoodsOrder.getSaleGoodsNum());
        Audit audit1 = auditService.selectAuditByOrderId(audit);
        //判断子集是否存在
        if (saleGoodsOrder.getSaleGoodsModels().get(0).getSaleGoodsModelId() == null) {
            //没有的话 给个空的集合
            List<SaleGoodsModel> list = new ArrayList();
            saleGoodsOrder.setSaleGoodsModels(list);
            resMap.put("saleGoodsOrder", saleGoodsOrder);
        } else {
            for (SaleGoodsModel saleGoodsModel : saleGoodsOrder.getSaleGoodsModels()) {
                if (saleGoodsModel.getUnit().equals("盒") || saleGoodsModel.getUnit() == "盒") {
                    saleGoodsModel.setAllNum(saleGoodsModel.getAllNum() / saleGoodsModel.getFactor().intValue());
                    saleGoodsModel.setAmount(saleGoodsModel.getAmount() / saleGoodsModel.getFactor().intValue());
                }
            }
            resMap.put("saleGoodsOrder", saleGoodsOrder);
        }
        SysUser userN = userService.selectUserById(saleGoodsOrder.getUserId());
        Company companyInvoice = iCompanyServer.selectByCompanyId(saleGoodsOrder.getDeptId());

        saleGoodsOrder.setCompanyAdress(companyInvoice.getOtherCorporateAddress());
        saleGoodsOrder.setComPhone(companyInvoice.getCorporateRegisterPhone());
        SysUser user1 = userService.selectUserByDeptId(sysUser.getCompanyId());

        resMap.put("companyName", user1.getNickName());
        List<SysUser> sysUsers = iSysUserService.selectSupplierById(companyId);
        List<SysUser> user = iSysUserService.selectSalesmanList(companyId);
        Product product = new Product();
        product.setDeptId(companyId);
        resMap.put("deptId", companyId);

        Company company = iCompanyServer.selectByCompanyId(companyId);
        resMap.put("address", company.getRasProvince() + company.getRasCity() + company.getRasDistrict() + company.getRasAddress());
        Company company1 = iCompanyServer.selectByCompanyId(saleGoodsOrder.getCustomerId());
        resMap.put("companyOhterAddress", company1.getRasProvince() + company1.getRasCity() + company1.getRasDistrict() + company1.getRasAddress());
        if (sysUsers != null) {
            resMap.put("sysUsers", sysUsers);
        }
        if (user != null) {
            resMap.put("user", user);
        }
        if (userN != null) {
            resMap.put("userName", userN.getNickName());
        }
        if (audit1 != null) {

            resMap.put("auditBy", audit1.getAuditBy());
        }
        String taxRate = saleGoodsOrder.getTaxRate();

        resMap.put("taxRate", taxRate);

        resMap.put("companyInvoice", companyInvoice);
        return resMap;
    }

    @GetMapping("/mtedit/{mateGoodsId}")
    public Map<String, Object> mtedit(@PathVariable("mateGoodsId") Long mateGoodsId) {
        Map<String, Object> resMap = new HashMap<>();

        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long companyId = sysUser.getCompanyId();

        MateOutbound mateOutbound1 = new MateOutbound();
        mateOutbound1.setDeptId(sysUser.getDeptId());
        mateOutbound1.setMateGoodsId(mateGoodsId);

        MateOutbound mateOutbound = mateOutboundMapper.selectMateOutboundBy(mateOutbound1);
        Audit audit = new Audit();
        Map<String, Object> mapa = new HashMap<>();
        mapa.put("materialId", mateOutbound.getMaterialId());
        mapa.put("companyId", SecurityUtils.getLoginUser().getCompanyId());
        MaterialManufactuter materialManufactuter = mateProcurementMapper.selctMateManufactuter(mapa);
        resMap.put("companyOhterAddress", materialManufactuter.getMateAddress());
        resMap.put("phone", materialManufactuter.getPhone());
        audit.setOrderId(mateGoodsId);
        audit.setOrderType("9");
        Audit audit1 = auditService.selectAuditByOrderId(audit);
        //判断子集是否存在
        if (mateOutbound.getMateOutboundModels().get(0).getMateGoodsModelId() == null) {
            //没有的话 给个空的集合
            List<MateOutboundModel> list = new ArrayList<>();
            mateOutbound.setMateOutboundModels(list);
            resMap.put("mateOutbound", mateOutbound);

        } else {
            resMap.put("mateOutbound", mateOutbound);
        }

        SysUser user1 = userService.selectUserByDeptId(sysUser.getCompanyId());
        resMap.put("companyName", user1.getNickName());
        Product product = new Product();
        product.setDeptId(companyId);
        resMap.put("deptId", companyId);

        Company company = iCompanyServer.selectByCompanyId(companyId);
        resMap.put("receiverAddress", company.getRasProvince() + company.getRasCity() + company.getRasDistrict() + company.getRasAddress());

        resMap.put("auditBy", audit1.getAuditBy());

        String taxRate = company.getTaxRate();

        resMap.put("taxRate", taxRate);

        //   resMap.put("companyInvoice", companyInvoice);
        return resMap;
    }
}
